package com.example.administrator.officeplatform.Activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.example.administrator.officeplatform.MyApplication;
import com.example.administrator.officeplatform.R;
import com.example.administrator.officeplatform.base.BaseActivity;
import com.example.administrator.officeplatform.kj.IpUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ChangePosswordActivity extends BaseActivity {
    private Button btn_save, btn_back;
    private Context mContext;
    private TextView et_oldPassword, et_password, et_passwords;
    private Boolean pd = true;
    private OkHttpClient mOkHttpClient= new OkHttpClient();
    private ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_change_possword);

    }

    @Override
    protected void initViews() {
        btn_back = (Button) findViewById(R.id.btn_back);
        btn_save = (Button) findViewById(R.id.btn_save);
        et_oldPassword = (TextView) findViewById(R.id.et_oldPassword);
        et_password = (TextView) findViewById(R.id.et_password);
        et_passwords = (TextView) findViewById(R.id.et_passwords);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvents() {
        btn_back.setOnClickListener(new MyOnClick());
        btn_save.setOnClickListener(new MyOnClick());
    }


    class MyOnClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
                case R.id.btn_back:
                    finish();
                    break;
                case R.id.btn_save:
                    modifypassword();
                    break;
            }
        }
    }

    public void modifypassword() {
        // 验证数据
        String oldPassword = et_oldPassword.getText().toString().trim();
        String password = et_password.getText().toString().trim();
        String passwords = et_passwords.getText().toString().trim();
        if (!password.endsWith(passwords)) {
            Toast.makeText(mContext, "密码两次输入不一致", Toast.LENGTH_SHORT).show();
            return;
        }
        sendExpense(oldPassword,password);

    }

    private void sendExpense(String old ,String newpassword){

        mProgressDialog = ProgressDialog.show(mContext, null, "正在加载中...");
        mProgressDialog.setCancelable(true);
        mProgressDialog.setCanceledOnTouchOutside(false);

        mOkHttpClient = new OkHttpClient();
        Map<String, Object> map = new HashMap<>();
        map.put("id", old);
        map.put("nextAuditorId", newpassword);
        String json1 = JSON.toJSONString(map);
        RequestBody formBody =  FormBody.create(MediaType.parse("application/json;charset=UTF-8"),json1);
        String s = MyApplication.getMsession();
        Request request = new Request.Builder()
                .url(IpUtil.getIP()+"/reimbursement/reimbursement/submitReimbursement")
                .header("cookie", MyApplication.getMsession())
                .addHeader("Content-Type","application/json;charset=UTF-8")
                .post(formBody)
                .build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                mProgressDialog.dismiss();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(mContext, "连接服务器异常", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                mProgressDialog.dismiss();

                String str = response.body().string();
                //{"messageType":2,"type":"success","title":"操作信息","message":"提交成功","data":null}
                if(str.indexOf("success")>=0){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(mContext, "提交成功", Toast.LENGTH_SHORT).show();
                            finish();

                        }
                    });
                }else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(mContext, "提交失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }

            }
        });
    }
}
